//
//  MSGInputInviteCodeViewController.m
//  ManShiGuang
//
//  Created by Teresa on 2020/5/10.
//  Copyright © 2020 lifayu. All rights reserved.
//

#import "MSGInputInviteCodeViewController.h"
#import "EJSUpdateUserInfoRequest.h"


@interface UIColor (Hex)
 
+ (UIColor *)colorWithHex:(long)hexColor;
+ (UIColor *)colorWithHex:(long)hexColor alpha:(float)opacity;
 
@end
 

@interface MSGInputInviteCodeViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textfield;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

@end

@implementation MSGInputInviteCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加邀请码";
    EJSUserTokenModel *model = [EJSUserTokenModel userTokenModel];
    if (model.salesmanCode.length > 0) {
        self.addBtn.hidden = YES;
        self.textfield.text = model.salesmanCode;
        self.textfield.userInteractionEnabled = NO;
    }else{
        self.addBtn.hidden = NO;
        self.textfield.text = nil;
        self.textfield.userInteractionEnabled = YES;
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.addBtn setTitleColor:[UIColor darkTextColor] forState:UIControlStateDisabled];
    [self.addBtn setTitleColor:[UIColor colorWithHex:0x4AA4FF] forState:UIControlStateNormal];
    [self btn:self.addBtn enabled:NO];
}

- (IBAction)addBtnClicked:(UIButton *)sender {
    [self.textfield resignFirstResponder];
    EJSUpdateUserInfoRequest *req = [EJSUpdateUserInfoRequest new];
    req.code = self.textfield.text;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        EJSUserTokenModel *model = [EJSUserTokenModel userTokenModel];
        model.salesmanCode = self.textfield.text;
        [model save];
        [[HUDHelper sharedInstance] tipMessage:@"邀请码添加成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (str.length > 0) {
        [self btn:self.addBtn enabled:YES];
    }else{
        [self btn:self.addBtn enabled:NO];
    }
    return YES;
}

-(void)btn:(UIButton*)btn enabled:(BOOL)enable{
    btn.enabled = enable;
    if (enable) {
        btn.borderColor = [UIColor colorWithHex:0x4AA4FF];
    }else{
        btn.borderColor = [UIColor darkTextColor];
    }
}

@end

 
 
@implementation UIColor (Hex)
 
+ (UIColor*) colorWithHex:(long)hexColor;{
    return [UIColor colorWithHex:hexColor alpha:1.];
}
 
+ (UIColor *)colorWithHex:(long)hexColor alpha:(float)opacity
{
    float red = ((float)((hexColor & 0xFF0000) >> 16))/255.0;
    float green = ((float)((hexColor & 0xFF00) >> 8))/255.0;
    float blue = ((float)(hexColor & 0xFF))/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:opacity];
}

@end
